import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React from "react";
// import PageContainer from "@/compoents/container";
import AccountLayout from "../../compoents/account-layout";
import styles from "./forgetPassword.module.less";
import useSuperLock from "../../hooks/useSuperLock";
import useCallBackState from "../../hooks/useCallBackState";
import { useTranslation } from "react-i18next";
import toast from "../../compoents/toast";
import VerificationCode from "../../compoents/verifiaction-code";
import LinearInput from "../../compoents/linear-input";
import classnames from "classnames";
import PhoneInput from "../../compoents/phone-input";
import { forgetPass } from "../../api/user";
import useServices from "../../hooks/useRequest";
import { useDispatch } from "react-redux";
import images from "../../assets";
import Loading from "../../compoents/s-loading";
import utilsBase from "../../utils/basc";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function forgetPassword() {
    var handleSwitch = function handleSwitch() {
        setState({
            isEmail: !state.isEmail,
            emailError: false
        });
    };
    var _useCallBackState = _sliced_to_array(useCallBackState({
        isEmail: true,
        email: "",
        area: "+86",
        phone: "",
        password: "",
        code: "",
        emailError: false,
        passwordError: false
    }), 2), state = _useCallBackState[0], setState = _useCallBackState[1];
    var t = useTranslation().t;
    var services = useServices({
        forgetPass: forgetPass
    });
    var dispatch = useDispatch();
    var _useSuperLock = _sliced_to_array(useSuperLock(/*#__PURE__*/ _async_to_generator(function() {
        var data;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    if (state.password.trim().length < 8) {
                        toast.createToast({
                            message: t("signIn0")
                        });
                        return [
                            2
                        ];
                    }
                    data = {
                        // email:state.email,
                        verificationCode: state.code,
                        password: state.password,
                        email: undefined,
                        areaCode: undefined,
                        phone: undefined,
                        loginName: undefined
                    };
                    if (state.isEmail) {
                        if (!utilsBase.checkEmail(state.email)) {
                            setState({
                                emailError: true
                            });
                            toast.createToast({
                                type: "errToast",
                                message: "Enter a valid Email!"
                            });
                            return [
                                2
                            ];
                        }
                        data.loginName = state.email;
                    } else {
                        data.loginName = "".concat(state.area, "-").concat(state.phone);
                    }
                    return [
                        4,
                        services.forgetPass(data, false).then(function(res) {
                            if (res.code === 200) {
                                toast.createToast({
                                    message: t("toast3")
                                });
                                dispatch.app.PUSH("/login");
                            } else {
                                toast.createToast({
                                    message: t("forgetPwd0"),
                                    type: "errToast"
                                });
                            }
                        })
                    ];
                case 1:
                    _state.sent();
                    return [
                        2
                    ];
            }
        });
    })), 2), handleForgetPwd = _useSuperLock[0], loading = _useSuperLock[1];
    var checkEmail = function() {
        if (!state.email) return;
        if (utilsBase.checkEmail(state.email)) {
            setState({
                emailError: false
            });
        } else {
            setState({
                emailError: true
            });
            toast.createToast({
                type: "errToast",
                message: "Enter a valid Email!"
            });
        }
    };
    var checkPassword = function() {
        if (!state.password) return;
        if (state.password.length < 8 || state.password.length > 16) {
            toast.createToast({
                type: "errToast",
                message: "The password must be at least 8 characters!"
            });
            setState({
                passwordError: true
            });
            return;
        }
        if (!/^[a-zA-Z0-9]{8,16}$/.test(state.password)) {
            toast.createToast({
                type: "errToast",
                message: "The password must be numbers or letters!"
            });
            setState({
                passwordError: true
            });
            return;
        }
        setState({
            passwordError: false
        });
    };
    var canSelect = (state.isEmail ? state.email.trim().length > 0 : state.phone.trim().length > 0) && state.password.trim().length > 0 && state.code.trim().length > 0 && !state.emailError && !state.passwordError;
    return /*#__PURE__*/ _jsx(AccountLayout, {
        backFn: function() {
            dispatch.app.BACK();
        },
        children: /*#__PURE__*/ _jsx("div", {
            className: styles.page,
            children: /*#__PURE__*/ _jsxs("div", {
                className: styles.content,
                children: [
                    /*#__PURE__*/ _jsxs("div", {
                        className: classnames(styles.flex, styles.signInBox),
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: styles.signIn,
                                children: t("loginHeader5")
                            }),
                            /*#__PURE__*/ _jsxs("div", {
                                style: {
                                    display: "flex",
                                    alignItems: "center"
                                },
                                onClick: function() {
                                    return handleSwitch();
                                },
                                children: [
                                    /*#__PURE__*/ _jsx("div", {
                                        className: styles.mobile,
                                        children: state.isEmail ? t("loginHeader1") : t("loginHeader0")
                                    }),
                                    /*#__PURE__*/ _jsx("img", {
                                        src: images.switch,
                                        className: styles.switch
                                    })
                                ]
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsxs("div", {
                        className: styles.labelBox,
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: styles.label,
                                children: state.isEmail ? t("loginHeader0") : t("loginHeader1")
                            }),
                            state.isEmail ? /*#__PURE__*/ _jsx(LinearInput, {
                                value: state.email,
                                onChange: function(value) {
                                    return setState({
                                        email: value
                                    });
                                },
                                placeholder: t("loginHeader31"),
                                maxlength: 30,
                                onBlur: checkEmail,
                                error: state.emailError
                            }) : /*#__PURE__*/ _jsx(PhoneInput, {
                                value: state.phone,
                                onChange: function(area, phone) {
                                    return setState({
                                        area: area,
                                        phone: phone
                                    });
                                },
                                maxlength: 15
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsxs("div", {
                        className: styles.labelBox,
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: styles.label,
                                children: t("loginHeader6")
                            }),
                            /*#__PURE__*/ _jsx(LinearInput, {
                                value: state.password,
                                onChange: function(value) {
                                    return setState({
                                        password: value
                                    });
                                },
                                placeholder: t("loginHeader33"),
                                maxlength: 16,
                                type: "safe-password",
                                password: true,
                                onBlur: checkPassword,
                                error: state.passwordError
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        className: styles.label,
                        children: t("loginHeader35")
                    }),
                    /*#__PURE__*/ _jsx(VerificationCode, {
                        target: state.isEmail ? !state.emailError ? state.email : "" : state.area + "-" + state.phone,
                        canSendValid: function(s) {
                            if (state.isEmail) {
                                return !utilsBase.checkEmail(s);
                            }
                            return false;
                        },
                        errMessage: state.isEmail ? "email cannot be empty" : "phoneNumber cannot be empty",
                        value: state.code,
                        onChange: function(value) {
                            return setState({
                                code: value
                            });
                        }
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        className: styles.btn,
                        style: canSelect ? {
                            background: "var(--primary)"
                        } : {},
                        onClick: function() {
                            return canSelect ? handleForgetPwd() : {};
                        },
                        children: loading ? /*#__PURE__*/ _jsx(Loading, {
                            color: "#fff"
                        }) : t("loginHeader26")
                    })
                ]
            })
        })
    });
}
export default forgetPassword;

 //# sourceMappingURL=index.js.map